package com.dzm.test08.ui;

import com.dzm.test08.entity.Student;
import com.dzm.test08.manager.StudentManager;
import com.dzm.test08.manager.UserManager;

import java.util.Scanner;

/**
 * 作者：Dzm
 * 时间：2024-07-13-10:09
 * 功能：
 */
public class StudentUi {
    private UserManager userManager = new UserManager();
    private StudentManager studentManager = new StudentManager();

    public void login() {
        //定义键盘扫描器
        Scanner scanner = new Scanner(System.in);
        //登录页展示
        System.out.println("-----------⭐⭐⭐ 欢迎使用合众学生管理系统-登录程序 ⭐⭐⭐-----------");
        System.out.print("请输入登录用户名：");
        String username = scanner.next();
        System.out.print("请输入登录密码：");
        String password = scanner.next();

        //登录操作，判断是否正常登录
        boolean l = userManager.login(username, password);
        if(!l) {
            System.out.println("对不起，未登陆成功，请重新输入！");
            login();
        }else {
            //登录成功展示菜单
            showMenu(scanner);
        }

    }

    private void showMenu(Scanner scanner) {
        System.out.println("==================== 合众学生管理系统 ====================");
        System.out.println("1.添加学生");
        System.out.println("2.查看学生");
        System.out.println("3.查找学生");
        System.out.println("4.修改学生");
        System.out.println("5.退出系统");
        System.out.println("=========================================================");

        userSelect(scanner);
    }

    private void userSelect(Scanner scanner) {
        System.out.print("请输入：");
        int choice = scanner.nextInt();

        switch (choice) {
            case 1:
                addStudent(scanner);
                break;
            case 2:
                printStudents();
                break;
            case 3:
                searchStudents(scanner);
                break;
            case 4:
                updateStudent(scanner);
                break;
            case 5:
                System.out.println("退出成功！");
                System.exit(0);
                break;
            default:
                System.out.println("\n请正确输入！\n");
        }

        System.out.println("是否继续?（y/n）");
        String answer = scanner.next();
        if(answer.equalsIgnoreCase("y")) {
            userSelect(scanner);
        }
    }

    //修改学生
    private void updateStudent(Scanner scanner) {
        System.out.print("请输入要修改的学生学号：");
        int id = scanner.nextInt();
        Student student = studentManager.findStudentById(id);
        if(student != null) {
            System.out.print("请输入要修改的名称：");
            String name = scanner.next();
            System.out.print("请输入要修改的年龄：");
            int age = scanner.nextInt();
            System.out.print("请输入要修改的住址：");
            String addr = scanner.next();
            System.out.print("请输入要修改的班级编号：");
            int cid = scanner.nextInt();

            student.setSname(name);
            student.setAge(age);
            student.setAddr(addr);
            student.setCid(cid);

            System.out.println("修改成功！");
        }else {
            System.out.println("未找到该学生！");
        }

    }

    //查找
    private void searchStudents(Scanner scanner) {
        System.out.print("请输入查找关键字：");
        String keyword = scanner.next();

        studentManager.findStudentsByKeyword(keyword);
    }

    //查看
    private void printStudents() {
        studentManager.searchStudents();
    }

    //添加
    private void addStudent(Scanner scanner) {
        System.out.print("请输入学号：");
        int sid = scanner.nextInt();
        System.out.print("请输入姓名：");
        String name = scanner.next();
        System.out.print("请输入性别：");
        String sex = scanner.next();
        System.out.print("请输入年龄：");
        int age = scanner.nextInt();
        System.out.print("请输入地址：");
        String addr = scanner.next();
        System.out.print("请输入班级编号：");
        int cid = scanner.nextInt();

        Student student = new Student(sid, name, sex, age, addr, cid);
        studentManager.addStudent(student);
    }
}
